package com.jkhan.common.exception.handler;

import com.jkhan.common.exception.BusinessException;
import com.jkhan.common.result.ResponseEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName GlobalException
 * @Description
 * @Author hjk
 * @date 2023/3/4 16:52
 * @Version 1.0
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseEntity<Object> error(Exception e) {
        log.error(e.getMessage(), e);
        return ResponseEntity.error().msg("发生了异常");
    }

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public ResponseEntity<Object> error(BusinessException e) {
        log.error(e.getMsg(), e);
        return ResponseEntity.fail().msg(e.getMsg()).code(e.getCode());
    }

}
